/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.controller;

import com.google.common.collect.Sets;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.vacoor.nothing.common.util.Oid;
import org.vacoor.nothing.security.entity.Module;
import org.vacoor.nothing.security.entity.Permission;
import org.vacoor.nothing.web.captcha.ImageCaptchaService;
import org.vacoor.nothing.web.util.servlet.Cookie;
import org.vacoor.nothing.web.util.servlet.SimpleCookie;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Locale;
import java.util.Set;

/*
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.Http11AprProtocol;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.coyote.http11.Http11Protocol;
*/

/**
 */
@Controller
@RequestMapping("/")
public class SecurityController {
    public static final String DEFAULT_LOGIN_CAPTCHA_COOKIE_NAME = "tcsid";

    private Cookie loginCaptchaCookie;
    @Autowired
    private ImageCaptchaService imageCaptchaService;

    public SecurityController() {
        loginCaptchaCookie = new SimpleCookie(DEFAULT_LOGIN_CAPTCHA_COOKIE_NAME);
    }


    /**
     * captcha
     */
    @RequestMapping("captcha.png")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // Set to expire far in the past.
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");

        String captchaId = Oid.get().toHexString();
        loginCaptchaCookie.setValue(captchaId);
        loginCaptchaCookie.saveTo(request, response);

        BufferedImage challenge = imageCaptchaService.getChallengeFor(captchaId);

        response.setContentType("image/png");
        ImageIO.write(challenge, "PNG", response.getOutputStream());
    }

    /**
     * public key
     */
    @RequestMapping("rsa.pub")
    public String getPublicKey() {
        return null;
    }

    @RequestMapping(value = {"{login:login;?.*}"})
    public String login(@RequestParam(value = "t", required = false) String t,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap model,
                        Locale locale) throws IOException {
        /*
        String captchaId = cookie.readValue(request, response);
        boolean valid = imageCaptchaService.validate(captchaId, request.getParameter("kapkey"));
        System.out.println("captcha valid:" + valid);
        */

        Subject subject = SecurityUtils.getSubject();
        return subject != null && subject.getPrincipal() != null ? "redirect:/index" : "login";
    }

    @RequestMapping("modules")
    public String getModules() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        @SuppressWarnings("unchecked")
        Set<Permission> perms = (Set<Permission>) session.getAttribute("");

        Set<Module> modules = Sets.newHashSet();
        Module root = new Module();

        return null;
    }
}
